/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IChangeMapperAssembler;
import com.inspur.edp.bff.api.manager.context.ChangeMapperContext;
import com.inspur.edp.bff.spi.action.changemapping.ChangeMappingAction;
import com.inspur.edp.bff.spi.action.changemapping.ChangeReversalMappingAction;
import com.inspur.edp.bff.spi.action.changemapping.DefaultChangeMapAction;
import com.inspur.edp.bff.spi.action.changemapping.DefaultChangeReversalAction;

import java.util.ArrayList;

public abstract class AbstractChangeMapperAssembler implements IChangeMapperAssembler {

    protected abstract ArrayList<ChangeMappingAction> getChangeMappingActions(ChangeMapperContext mappingContext);

    protected abstract ArrayList<ChangeReversalMappingAction> getChangeReversalMappingActions(ChangeMapperContext mappingContext);

    public final void doChangeMappingFromVo(IFSManagerContext context, ChangeMapperContext mappingContext) {
        ArrayList<ChangeReversalMappingAction> actions = getChangeReversalMappingActions(mappingContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<ChangeReversalMappingAction>();
            DefaultChangeReversalAction action = new DefaultChangeReversalAction(mappingContext);
            actions.add(action);
        }

        for (ChangeReversalMappingAction action : actions) {
            action.deal(context);
        }
    }

    public final void doChangeMapping2Vo(IFSManagerContext context, ChangeMapperContext mappingContext) {
        ArrayList<ChangeMappingAction> actions = getChangeMappingActions(mappingContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<ChangeMappingAction>();
            DefaultChangeMapAction action = new DefaultChangeMapAction(mappingContext);
            actions.add(action);
        }

        for (ChangeMappingAction action : actions) {
            action.deal(context);
        }
    }

}
